<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"
            "http://www.w3.org/TR/REC-html40/loose.dtd">
<HTML>
<HEAD>



<META http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<META name="GENERATOR" content="hevea 1.08">
<LINK rel="stylesheet" type="text/css" href="libman.css">
<TITLE>
More on the CHR Language
</TITLE>
</HEAD>
<BODY >
<A HREF="libman046.html"><IMG SRC ="previous_motif.gif" ALT="Previous"></A>
<A HREF="libman042.html"><IMG SRC ="contents_motif.gif" ALT="Up"></A>
<A HREF="libman048.html"><IMG SRC ="next_motif.gif" ALT="Next"></A>
<HR>

<H2 CLASS="section"><A NAME="htoc86">8.5</A>&nbsp;&nbsp;More on the <FONT COLOR=purple>CHR</FONT> Language</H2><UL>
<LI><A HREF="libman047.html#toc41">Declarations</A>
<LI><A HREF="libman047.html#toc42">ECL<SUP><I>i</I></SUP>PS<SUP><I>e</I></SUP> Clauses</A>
<LI><A HREF="libman047.html#toc43">Options</A>
<LI><A HREF="libman047.html#toc44"><FONT COLOR=purple>CHR</FONT> Built-In Predicates</A>
</UL>

The following subsections describe declarations, clauses, options and built-in
predicates of the <FONT COLOR=purple>CHR</FONT> language.<BR>
<BR>
<A NAME="toc41"></A>
<H3 CLASS="subsection"><A NAME="htoc87">8.5.1</A>&nbsp;&nbsp;Declarations</H3><A NAME="@default238"></A>
Declarations name the constraint handler, its constraints, specify
their syntax and use in built-in labeling.<BR>
<BR>
<DIV CLASS="center">
<TABLE BORDER=1 CELLSPACING=0 CELLPADDING=1>
<TR><TD ALIGN=left NOWRAP>Declaration</TD>
<TD VALIGN=top ALIGN=center NOWRAP>&nbsp;::=&nbsp;</TD>
<TD ALIGN=left NOWRAP><CODE>handler</CODE> Name<CODE>.</CODE></TD>
</TR>
<TR><TD ALIGN=left NOWRAP>&nbsp;</TD>
<TD VALIGN=top ALIGN=center NOWRAP>&nbsp;::=&nbsp;</TD>
<TD ALIGN=left NOWRAP><CODE>constraints</CODE> SpecList<CODE>.</CODE></TD>
</TR>
<TR><TD ALIGN=left NOWRAP>&nbsp;</TD>
<TD VALIGN=top ALIGN=center NOWRAP>&nbsp;::=&nbsp;</TD>
<TD ALIGN=left NOWRAP><CODE>operator(</CODE>Precedence<CODE>,</CODE>Associativity<CODE>,</CODE>Name<CODE>).</CODE></TD>
</TR>
<TR><TD ALIGN=left NOWRAP>&nbsp;</TD>
<TD VALIGN=top ALIGN=center NOWRAP>&nbsp;::=&nbsp;</TD>
<TD ALIGN=left NOWRAP><CODE>label_with</CODE> Constraint <CODE>if</CODE> Guard<CODE>.</CODE></TD>
</TR></TABLE>
</DIV><BR>
<BR>
The optional <CODE>handler</CODE> declaration<A NAME="@default239"></A> documents the name of the constraint
handler. Currently it can be omitted, but will be useful in future releases for
combining handlers.<BR>
<BR>
The mandatory <CODE>constraints</CODE> declaration<A NAME="@default240"></A> lists the constraints
defined in the handler. A &#8220;SpecList&#8221; is a list of Name<CODE>/</CODE>Arity
pairs for the constraints. 
The declaration of a constraint <EM>must appear before</EM>
the constraint handling rules and ECL<SUP><I>i</I></SUP>PS<SUP><I>e</I></SUP> clauses which define it,
otherwise a syntax error is raised.
There can be several <TT>constraints</TT> declarations. <BR>
<BR>
The optional <CODE>operator</CODE> declaration<A NAME="@default241"></A> declares an operator, with
the same arguments as <TT>op/3</TT> in ECL<SUP><I>i</I></SUP>PS<SUP><I>e</I></SUP>. However, while the usual
operator declarations are ignored during compilation from <TT>chr</TT> to
<TT>pl</TT> files, the <FONT COLOR=purple>CHR</FONT> operator declarations are taken into account
(see also the subsection on clauses).<BR>
<BR>
The optional <CODE>label_with</CODE> declaration<A NAME="@default242"></A> specifies when the
ECL<SUP><I>i</I></SUP>PS<SUP><I>e</I></SUP> clauses of a constraint can be used for built-in labeling
(see subsection on labeling). <BR>
<BR>
<B>Example, contd.:</B> The first lines of the minmax handler are
declarations: <PRE CLASS="verbatim">
handler minmax.

constraints leq/2, neq/2, minimum/3, maximum/3.

operator(700, xfx, leq).
operator(700, xfx, neq).
</PRE>
<A NAME="toc42"></A>
<H3 CLASS="subsection"><A NAME="htoc88">8.5.2</A>&nbsp;&nbsp;ECL<SUP><I>i</I></SUP>PS<SUP><I>e</I></SUP> Clauses</H3>
A constraint handler program may also include arbitrary ECL<SUP><I>i</I></SUP>PS<SUP><I>e</I></SUP> code (written
with the four operators <CODE>:- /[1,2]</CODE> and <CODE>?- /[1,2]</CODE>).
<DIV CLASS="center">
<TABLE BORDER=1 CELLSPACING=0 CELLPADDING=1>
<TR><TD ALIGN=left NOWRAP>Clause</TD>
<TD VALIGN=top ALIGN=center NOWRAP>&nbsp;::=&nbsp;</TD>
<TD ALIGN=left NOWRAP>Head <CODE>:-</CODE> Body.</TD>
</TR>
<TR><TD ALIGN=left NOWRAP>&nbsp;</TD>
<TD VALIGN=top ALIGN=center NOWRAP>&nbsp;::=&nbsp;</TD>
<TD ALIGN=left NOWRAP>Head <CODE>?-</CODE> Body.</TD>
</TR>
<TR><TD ALIGN=left NOWRAP>&nbsp;</TD>
<TD VALIGN=top ALIGN=center NOWRAP>&nbsp;::=&nbsp;</TD>
<TD ALIGN=left NOWRAP><CODE>:-</CODE> Body.</TD>
</TR>
<TR><TD ALIGN=left NOWRAP>&nbsp;</TD>
<TD VALIGN=top ALIGN=center NOWRAP>&nbsp;::=&nbsp;</TD>
<TD ALIGN=left NOWRAP><CODE>?-</CODE> Body.</TD>
</TR></TABLE>
</DIV><BR>
<BR>
Note that <TT>:-/1</TT> and <TT>?-/1</TT> <EM>behave different from each other</EM> in <FONT COLOR=purple>CHR</FONT> programs.
Clauses starting with <TT>:-</TT> are <EM>copied</EM> into the <TT>pl</TT>
file by the <FONT COLOR=purple>CHR</FONT> compiler, clauses with <TT>?-</TT> are <EM>executed</EM>
by the compiler. As the <TT>op</TT> declaration needs both copying and
execution, we have introduced the special <TT>operator</TT> declaration
(see previous subsection on declarations). A "Head" can be a "Constraint",
such clauses are used for built-in labeling only (see section on labeling).<BR>
<BR>
<A NAME="toc43"></A>
<H3 CLASS="subsection"><A NAME="htoc89">8.5.3</A>&nbsp;&nbsp;Options</H3><A NAME="@default243"></A>
The <TT>option</TT> command allows the user to set options in the <FONT COLOR=purple>CHR</FONT>
compiler. <BR>
<BR>
<DIV CLASS="center">
<TABLE BORDER=1 CELLSPACING=0 CELLPADDING=1>
<TR><TD ALIGN=left NOWRAP>Option</TD>
<TD VALIGN=top ALIGN=center NOWRAP>&nbsp;::=&nbsp;</TD>
<TD ALIGN=left NOWRAP><CODE>option(</CODE>Option<CODE>,</CODE> On_or_off<CODE>).</CODE></TD>
</TR></TABLE>
</DIV><BR>
<BR>
Options can be switched on or off. <EM>Default is</EM> <TT>on</TT>.
Advanced users may switch an option off to improve the efficiency
of the handler at the cost of safety. Options are:
<UL CLASS="itemize"><LI CLASS="li-itemize">
<CODE>check_guard_bindings</CODE><A NAME="@default244"></A>:
When executing a guard<A NAME="@default245"></A>, it is checked that no global variables
(variables of the rule heads) are touched (see subsection on how
<FONT COLOR=purple>CHR</FONT>s work). If the option is on, guards involving cut, if-then-else
or negation may not work correctly if a global variable has been touched before.
If switched off, guard checking may be significantly
faster, but only safe if the user makes sure that global variables are
not touched. To ensure that the variables are sufficiently bound,
tests like <TT>nonvar/1</TT> or delays can be added to the predicates
used in the guards.<BR>
<BR>
<LI CLASS="li-itemize"><CODE>already_in_store</CODE><A NAME="@default246"></A>: 
Before adding a user-defined constraint to the constraint store, it is
checked if there is an identical one already in the store. If there
is, the new constraint needs not to be added. The handling of the
duplicate constraint is avoided. This option can be set to <CODE>off</CODE>,
because the checking may be too expensive if duplicate constraints
rarely occur. Specific duplicate constraints can still be removed by
a simpagation rule of the form <CODE>Constraint \ Constraint &lt;=&gt; true</CODE>.<BR>
<BR>
<LI CLASS="li-itemize"><CODE>already_in_heads</CODE><A NAME="@default247"></A>: 
In two-headed simplification rules, the intention is often to simplify
the two head constraints into a stronger version of one of the
constraints. However, a straightforward encoding of the rule may
include the case where the new constraint is identical to the
corresponding head constraint. Removing the head constraint and
adding it again in the body is inefficient and may cause termination
problems. If the <CODE>already_in_heads</CODE> option is on, in such a case
the head constraint is kept and the body constraint ignored. Note
however, that this optimization currently <EM>only works if</EM> the body
constraint is the only goal of the body or the first goal in the
conjunction comprising the body of the rule (see the example handler
for domains). The option may be too expensive if identical head-body
constraints rarely occur.<BR>
<BR>
<LI CLASS="li-itemize">Note that the ECL<SUP><I>i</I></SUP>PS<SUP><I>e</I></SUP> environment flag <CODE>debug_compile</CODE> (set and
unset with <CODE>dbgcomp</CODE> and <CODE>nodbgcomp</CODE>)<A NAME="@default248"></A><A NAME="@default249"></A><A NAME="@default250"></A> is also taken into
account by the <FONT COLOR=purple>CHR</FONT> compiler. The default is <TT>on</TT>.
If switched off, the resulting code is more
efficient, but cannot be debugged anymore (see section <A HREF="libman050.html#chrdebug">8.8</A>).</UL>
<A NAME="toc44"></A>
<H3 CLASS="subsection"><A NAME="htoc90">8.5.4</A>&nbsp;&nbsp;<FONT COLOR=purple>CHR</FONT> Built-In Predicates</H3>
There are some built-in predicates to compile <TT>chr</TT> files, for
debugging, built-in labeling and to
inspect the constraint store and remove its constraints:
<UL CLASS="itemize"><LI CLASS="li-itemize"><TT>chr2pl(</TT>File)<A NAME="@default251"></A> compiles &#8220;File&#8221; from a <TT>chr</TT> to <TT>pl</TT> file. <BR>
<BR>
<LI CLASS="li-itemize"><TT>chr(</TT>File)<A NAME="@default252"></A> compiles &#8220;File&#8221; from a <TT>chr</TT> to
<TT>pl</TT> file and loads the <TT>pl</TT> file. <BR>
<BR>
<LI CLASS="li-itemize"><CODE>chr_trace</CODE><A NAME="@default253"></A> activates the standard debugger and
shows constraint handling.
<LI CLASS="li-itemize"><CODE>chr_notrace</CODE><A NAME="@default254"></A> stops either debugger.<BR>
<BR>
<LI CLASS="li-itemize"><TT>chr_labeling</TT><A NAME="@default255"></A>
provides built-in labeling (see corresponding subsection). 
<LI CLASS="li-itemize"><TT>chr_label_with(</TT>Constraint)<A NAME="@default256"></A> checks if &#8220;Constraint&#8221; satisfies a
<TT>label_with</TT> declaration (used for built-in labeling). 
<LI CLASS="li-itemize"><TT>chr_resolve(</TT>Constraint)<A NAME="@default257"></A> uses the ECL<SUP><I>i</I></SUP>PS<SUP><I>e</I></SUP> 
clauses to solve a constraint (used for built-in labeling).<BR>
<BR>
<LI CLASS="li-itemize"><TT>chr_get_constraint(</TT>Constraint)<A NAME="@default258"></A>
gets a constraint unifying with &#8220;Constraint&#8221; from the constraint
store and removes it, gets another constraint on backtracking.
<LI CLASS="li-itemize"><TT>chr_get_constraint(</TT>Variable,Constraint)<A NAME="@default259"></A> is the same as 
<TT>chr_get_constraint/1</TT> except that the constraint constrains the variable
&#8220;Variable&#8221;.</UL>
<HR>
<A HREF="libman046.html"><IMG SRC ="previous_motif.gif" ALT="Previous"></A>
<A HREF="libman042.html"><IMG SRC ="contents_motif.gif" ALT="Up"></A>
<A HREF="libman048.html"><IMG SRC ="next_motif.gif" ALT="Next"></A>
</BODY>
</HTML>
